using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace Pix2Gallery2
{
    public partial class GalleryURLForm : Form
    {
        private Pix2Gallery2Settings m_pix2gallery2Settings;
        private Gallery2Url m_g2Url;

        public Pix2Gallery2Settings pix2gallery2Settings
        {
            get
            {
                return m_pix2gallery2Settings;
            }
            set
            {
                m_pix2gallery2Settings = value;
            }
        }

        public GalleryURLForm()
        {
            InitializeComponent();
        }

        private void GalleryURLForm_Load(object sender, EventArgs e)
        {
            if (this.m_pix2gallery2Settings == null)
            {
                MessageBox.Show("ERROR: Settings not loaded!");
                this.Close();
            }

            if (this.textBoxGalleryTitle.Text != "")
            {
                foreach (Gallery2Url g2Url in this.m_pix2gallery2Settings.Gallery2Urls)
                {
                    if (g2Url.m_GalleryTitle == this.textBoxGalleryTitle.Text)
                    {
                        this.textBoxGalleryURL.Text = g2Url.m_GalleryUrl;
                        this.textBoxGalleryUsername.Text = g2Url.m_GalleryUserName;
                        this.textBoxGalleryPassword.Text = g2Url.m_GalleryPassword;
                        m_g2Url = g2Url;
                        break;
                    }
                }
            }
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (this.textBoxGalleryTitle.Text == "")
            {
                MessageBox.Show("You must provide a title for this Gallery URL.");
                return;
            }

            //Verify the URL format...

            if (this.textBoxGalleryURL.Text == "")
            {
                MessageBox.Show("You must provide a URL for this Gallery URL.");
                return;
            }
            try
            {
                Uri g2Uri = new Uri(this.textBoxGalleryURL.Text);
            }
            catch(UriFormatException ex)
            {
                MessageBox.Show("The Gallery URL must be formatted correctly.\nException Message: " + ex.Message);
                return;
            }
            
            if (this.textBoxGalleryUsername.Text == "")
            {
                MessageBox.Show("You must provide a username for this Gallery URL.");
                return;
            }

            //Consider verifying that the username/password combination is correct...
            
            Gallery2Url g2Url;
            
            //Handle the editing option...
            if (this.m_g2Url == null) g2Url = new Gallery2Url();
            else g2Url = this.m_g2Url;

            g2Url.m_GalleryTitle = this.textBoxGalleryTitle.Text;
            g2Url.m_GalleryUrl = this.textBoxGalleryURL.Text;
            g2Url.m_GalleryUserName = this.textBoxGalleryUsername.Text;
            g2Url.m_GalleryPassword = this.textBoxGalleryPassword.Text;

            if(m_pix2gallery2Settings.Gallery2Urls == null)
            {
                m_pix2gallery2Settings.Gallery2Urls = new List<Gallery2Url>();
            }
            if (this.m_g2Url == null) m_pix2gallery2Settings.Gallery2Urls.Add(g2Url);

            m_pix2gallery2Settings.AppSettingsChanged = true;

            this.Close();
        }
    }
}